from django.test import LiveServerTestCase
from model_mommy import mommy
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from users.models import CCUser
from website.models import Artist, Album, Song, artist_path, album_path, song_path


class ViewTests(LiveServerTestCase):
    def setUp(self):
        #self.selenium = webdriver.Remote('localhost:8000', webdriver.DesiredCapabilities.HTMLUNIT.copy())
        #self.selenium = webdriver.Firefox()
        self.selenium = webdriver.Chrome()
        self.selenium.maximize_window()
        super(ViewTests, self).setUp()
        self.testuser = mommy.prepare(CCUser, username='somedude')
        self.test_password = '1l1k3ccr4d10'
        self.user = CCUser.objects.create_user(username=self.testuser.username, email='myfake@email.com', password=self.test_password)
        self.admin = CCUser.objects.create_superuser(username='admin', email='admin@admin.com', password='admin')
        self.artist1 = mommy.make(Artist)
        self.album11 = mommy.make(Album, artist=self.artist1)
        self.song111 = mommy.make(Song, artist=self.artist1, album=self.album11)
        self.song112 = mommy.make(Song, artist=self.artist1, album=self.album11)
        self.album12 = mommy.make(Album, artist=self.artist1)
        self.song121 = mommy.make(Song, artist=self.artist1, album=self.album12)
        self.song122 = mommy.make(Song, artist=self.artist1, album=self.album12)
        self.artist2 = mommy.make(Artist)
        self.album21 = mommy.make(Album, artist=self.artist2)
        self.song211 = mommy.make(Song, artist=self.artist2, album=self.album21)
        self.song212 = mommy.make(Song, artist=self.artist2, album=self.album21)
        self.album22 = mommy.make(Album, artist=self.artist2)
        self.song221 = mommy.make(Song, artist=self.artist2, album=self.album22)
        self.song222 = mommy.make(Song, artist=self.artist2, album=self.album22)


    def tearDown(self):
        self.selenium.quit()
        super(ViewTests, self).tearDown()


    def test_create_user(self):
        selenium = self.selenium
        selenium.get(self.live_server_url + '/users/signup/')
        createduser = mommy.prepare(CCUser)

        try:
            #WebDriverWait(selenium, 10).until(EC.title_contains('Sign Up'))

            username = selenium.find_element_by_id('id_username')
            email = selenium.find_element_by_id('id_email')
            password1 = selenium.find_element_by_id('id_password1')
            password2 = selenium.find_element_by_id('id_password2')

            username.send_keys(createduser.username)
            email.send_keys(createduser.email)
            password1.send_keys(self.test_password)
            password2.send_keys(self.test_password)
            username.submit()

            #WebDriverWait(selenium, 10).until(EC.title_contains('Login'))
            self.assertTrue('Login' in selenium.title)

        finally:
            selenium.quit()


    def test_login(self):
        selenium = self.selenium
        selenium.get(self.live_server_url + '/users/login/')
        
        try:
            #WebDriverWait(selenium, 10).until(EC.login_contains('Login'))
            
            username = selenium.find_element_by_id('id_username')
            password = selenium.find_element_by_id('id_password')
            username.send_keys(self.testuser.username)
            password.send_keys(self.test_password)
            username.submit()
            WebDriverWait(selenium, 360).until(EC.title_contains('CCRadio'))

            # Assertion: User is taken back to welcome page
            self.assertTrue('CCRadio' in selenium.title)
            # Assertion: User's username is listed in navbar
            selenium.switch_to.frame('navbar')
            usermenu = selenium.find_element_by_id('user-menu')
            self.assertTrue(self.testuser.username in usermenu.text)
            # Assertion: Logout link is shown in navbar
            logout_link = usermenu.find_element_by_link_text('Logout')
            self.assertTrue(logout_link.is_displayed())

        finally:
            selenium.quit()


    def test_artists_page(self):
        self.artists_page(False)
        self.artists_page(True)

    def artists_page(self, logged_in):
        if(logged_in):
            self.client.login(username=self.testuser.username, password=self.test_password)
        selenium = self.selenium
        selenium.get(self.live_server_url + '/artists')
        WebDriverWait(selenium, 360).until(EC.title_contains('Artists'))
        pagebody = selenium.find_element_by_id('artists_body')

        # There should be two artists.  Check that both are listed.
        self.assertTrue(self.artist1.name in pagebody.text)
        self.assertTrue(self.artist2.name in pagebody.text)


    def test_artist_page(self):
        self.artist_page(False)
        self.artist_page(True)

    def artist_page(self, logged_in):
        if(logged_in):
            self.client.login(username=self.testuser.username, password=self.test_password)
        for artist in Artist.objects.all():
            self.validate_artist_page(artist)

    def validate_artist_page(self, artist):
        selenium = self.selenium
        selenium.get(self.live_server_url + '/' + artist.handle)
        WebDriverWait(selenium, 360).until(EC.title_contains('Artist:'))
        pagebody = selenium.find_element_by_id('artist_body')
        for album in Album.objects.filter(artist=artist.handle):
            self.assertTrue(album.title in pagebody.text)


    def test_album_page(self):
        self.album_page(False)
        self.album_page(True)

    def album_page(self, logged_in):
        if(logged_in):
            self.client.login(username=self.testuser.username, password=self.test_password)
        for album in Album.objects.all():
            self.validate_album_page(album)

    def validate_album_page(self, album):
        selenium = self.selenium
        selenium.get(self.live_server_url + '/' + album.artist.handle + '/' + album.slug)
        WebDriverWait(selenium, 360).until(EC.title_contains('Album:'))
        pagebody = selenium.find_element_by_id('album_body')
        for song in Song.objects.filter(artist=album.artist, album=album):
            self.assertTrue(song.title in pagebody.text)
